package model;

/*
 * The Address class.
 * 
 * @author Abdiel Cabrera
 * 
 * This class holds the info of an Address object.
 */

/**
 * Address class.
 * 
 * @author Abdiel Cabrera.
 * @version 1.0
 * @date Winter 2014
 */
public class Address {

	/** The Address1 */
	private String my_address1 = "";

	/** The Address2 */
	private String my_address2 = "";

	/** The apartment number */
	private String my_apt_num = "";

	/** The city */
	private String my_city = "";

	/** The state */
	private String my_state = "";

	/** 
	 * The zip code. 
	 * 
	 * See {@linktour http://en.wikipedia.org/wiki/ZIP_code}
	 * 
	 */
	private String my_zip = "";

	/** 
	 * Construct a Address object. 
	 * 
	 * @pre the_address1 != null
	 * @pre the_address2 != null
	 * @pre the_apt_num != null
	 * @pre the_city != null
	 * @pre the_state != null
	 * @pre the_zip != null
	 * 
	 * @param the_address1 The address1
	 * @param the_address2 The address2
	 * @param the_apt_num The apt number
	 * @param the_city The city
	 * @param the_state The state
	 * @param the_zip The zip code
	 * 
	 * */
	public Address(String the_address1, String the_address2, String the_apt_num,
			String the_city, String the_state, String the_zip) {
		my_address1 = the_address1;
		my_address2 = the_address2;
		my_apt_num = the_apt_num;
		my_city = the_city;
		my_state = the_state;
		my_zip = the_zip;
	}

	/**
	 * Returns the Address1 
	 * 
	 * @return the my_address1
	 */
	public String getMy_address1() {
		return my_address1;
	}

	/**
	 * Sets the Address1
	 * 
	 * @pre the_address1 != null
	 * @param my_address1 the my_address1 to set
	 */
	public void setMy_address1(String the_address1) {
		this.my_address1 = the_address1;
	}

	/**
	 * Returns the Address2
	 * 
	 * @return the my_address2
	 */
	public String getMy_address2() {
		return my_address2;
	}

	/**
	 * Sets the Address2
	 * 
	 * @pre the_address2 != null
	 * @param my_address2 the my_address2 to set
	 */
	public void setMy_address2(String the_address2) {
		this.my_address2 = the_address2;
	}

	/**
	 * Returns the apartment number.
	 * 
	 * @return the my_apt_num
	 */
	public String getMy_apt_num() {
		return my_apt_num;
	}

	/**
	 * Sets the apartment number
	 * 
	 * @pre the_apt_num != null
	 * @param my_apt_num the my_apt_num to set
	 */
	public void setMy_apt_num(String the_apt_num) {
		this.my_apt_num = the_apt_num;
	}

	/**
	 * Returns the city.
	 * 
	 * @return the my_city
	 */
	public String getMy_city() {
		return my_city;
	}

	/**
	 * Sets the City.
	 * 
	 * @pre the_city != null
	 * @param my_city the my_city to set
	 */
	public void setMy_city(String the_city) {
		this.my_city = the_city;
	}

	/**
	 * Returns the state.
	 * 
	 * @return the my_state
	 */
	public String getMy_state() {
		return my_state;
	}

	/**
	 * Sets the state.
	 * 
	 * @pre the_state != null
	 * @param my_state the my_state to set
	 */
	public void setMy_state(String the_state) {
		this.my_state = the_state;
	}

	/**
	 * Return the zip code
	 * 
	 * @return the my_zip
	 */
	public String getMy_zip() {
		return my_zip;
	}

	/**
	 * Sets the zip code.
	 * 
	 * @pre the_zip != null
	 * @param my_zip the my_zip to set
	 */
	public void setMy_zip(String the_zip) {
		this.my_zip = the_zip;
	}

	/**
	 * Check if an specific address is valid or not.
	 * 
	 * @return TRUE if Address valid. 
	 *         FALSE otherwise. 
	 */
	public boolean isValid() {
		boolean answer = true;
		if (my_address1.equals("")) answer = false;
		if (my_city.equals("")) answer = false;
		if (my_state.equals("")) answer = false;
		if (my_state.length() != 2) answer = false;
		if (my_zip.length() != 5) answer = false;
		if (!isNumeric(my_zip)) answer = false;
		return answer;
	}

	/**
	 * Returns a String representation of the Address object.
	 * 
	 * @return a String representation.
	 */
	public  String toString()
	{
		return my_address1 + "~" + my_address2 + "~" + my_apt_num + "~" + my_city 
				+ "~" + my_state + "~" + my_zip;
	}

	// ***** Helper methods *****

	/**
	 * Checks if an specific String is numeric.
	 * 
	 * @pre str != null
	 * @return TRUE if numeric. 
	 *         FALSE otherwise. 
	 */
	private static boolean isNumeric(String str)  
	{  
		try  
		{  
			@SuppressWarnings("unused")
			double my_double = Double.parseDouble(str);
		}  
		catch(NumberFormatException nfe)  
		{  
			return false;  
		}  
		return true;  
	}
}
